const requireDirestory = require('require-directory')
const Router = require('koa-router')

class InitManager{
  static initCore (app) {
    // 入口方法
    InitManager.app = app
    InitManager.initLoadRouter ()
    InitManager.loadHttpException ()
    InitManager. loadConfig ()
  }

  static initLoadRouter () {
    requireDirestory(module, `${process.cwd()}/app/api`, {
      visit: (obj) => {
        if(obj instanceof Router) {
          InitManager.app.use(obj.routes())
        }
      }
    })
    
    // function whileLoadModule (obj) {
    //   if(obj instanceof Router) {
    //     app.use(obj.routes())
    //   }
    // }
  }

  static loadHttpException () {
    const errors = require('./http-exception')
    global.errs = errors
  }

  static loadConfig (path = '') {
    const configPath = path || process.cwd() + '/config/config.js'
    const config = require(configPath)
    global.config = config
  }
}

module.exports = InitManager